//
//  YSProductListItemCell.m
//  Yueshi
//
//  Created by Northwan on 15/7/21.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//

#import "YSProductListItemCell.h"

#define PromotionBtnHeight 25

@interface YSProductListItemCell()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UIImageView *businessIconView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *longNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *priceLabel;
@property (weak, nonatomic) IBOutlet UILabel *buyCountLabel;
@property (strong, nonatomic) UIButton *promotionBtn;
@property (weak, nonatomic) IBOutlet UILabel *oldPriceLabel;

@end

@implementation YSProductListItemCell

-(void)awakeFromNib{
    if(!self.promotionBtn){
        UIButton *promotionBtn = [[UIButton alloc]init];
        [promotionBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [promotionBtn.titleLabel setFont:SYS_FONT(14)];
        [promotionBtn setBackgroundImage:[UIImage resizedImage:@"bg_list_promotion"] forState:UIControlStateNormal];
        [promotionBtn setFrame:CGRectMake(0, YSPublicXInset, 50,PromotionBtnHeight)];
        [promotionBtn setHidden:YES];
        self.promotionBtn = promotionBtn;
        [self.iconView addSubview:promotionBtn];
    }
}


+(instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString *CellIdentiferId = @"YSProductListItemCell";
    YSProductListItemCell *cell =  [tableView dequeueReusableCellWithIdentifier:CellIdentiferId];
    if(cell == nil){
        NSArray *nibs = [[NSBundle mainBundle]loadNibNamed:@"YSProductListItemCell" owner:nil options:nil];
        cell = [nibs lastObject];
    }
    return cell;
}

-(void)setProd:(YSProductList *)prod{
    [self.iconView setImageURLStr:prod.prodImageUrl];
    
    if(prod.prodPromotion){
        [self.promotionBtn setHidden:NO];
        if(prod.prodPromotion.isPanicBuy){
            NSString *promotionPrice = nil;
            if(prod.prodPromotion.whetherShipping){
                promotionPrice = [NSString stringWithFormat:@"限时包邮¥%.1f",prod.prodPromotion.price];
            }else{
                promotionPrice = [NSString stringWithFormat:@"限时抢购¥%.1f",prod.prodPromotion.price];
            }
            [self.promotionBtn setTitle:promotionPrice forState:UIControlStateNormal];
            CGSize promotionPriceSize =   [promotionPrice sizeWithFont:SYS_FONT(14)];
            [self.promotionBtn setWidth:promotionPriceSize.width+YSPublicXInset*2];
        }else if(prod.prodPromotion.isPresent){
            NSString *presentStr = @"买就赠";
            [self.promotionBtn setTitle:presentStr forState:UIControlStateNormal];
            CGSize presentSize =  [presentStr sizeWithFont:SYS_FONT(14)];
            [self.promotionBtn setWidth:presentSize.width+YSPublicXInset*2];
        }
    }else{
        [self.promotionBtn setHidden:YES];
    }
    
    [self.nameLabel setText:prod.prodShortName];
    [self.longNameLabel setText:prod.prodWholeName];
    [self.businessIconView setImageURLStr:prod.supplierIcon];
    [self.priceLabel setText:[NSString stringWithFormat:@"¥%.1f",prod.prodNewPrice]];
    
    self.oldPriceLabel.hidden = YES;
    NSString *oldPrice = [NSString stringWithFormat:@"¥%.1f",prod.prodOldPrice];
    [UIUtil addMiddleLineWithLabel:self.oldPriceLabel text:oldPrice];
    
    [self.buyCountLabel setText:[NSString stringWithFormat:@"已售%ld",prod.prodBuyCount]];
}

@end
